//
//  QQUnZipViewController.swift
//  ZipTools
//
//  Created by 齐庆斌 on 2018/4/13.
//  Copyright © 2018年 齐庆斌. All rights reserved.
//

import UIKit
fileprivate let UnZipCellID = "UnzipTabViewCell"

class QQUnZipViewController: UIViewController {
    
    // 压缩文件列表
    lazy var files: [UnzipData] = {
        var paths = [UnzipData]()
        for i in 1...14 {
            let file = UnzipData()
            file.path = Bundle.main.path(forResource: "\(i)", ofType: "zip")!
            paths.append(file)
        }
        return paths
    }()
    
    lazy var tbView: UITableView = {
        let tbView = UITableView()
        tbView.frame = self.view.frame
        tbView.dataSource = self
        tbView.delegate = self
        
        tbView.separatorStyle = .none
        tbView.backgroundColor = UIColor.white
        
        tbView.register((UINib(nibName: UnZipCellID, bundle: nil)), forCellReuseIdentifier: UnZipCellID)
        
        
        return tbView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "压缩文件列表"
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(tbView)
        
    }
    
}

extension QQUnZipViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return files.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UnzipTabViewCell = tableView.dequeueReusableCell(withIdentifier: UnZipCellID) as! UnzipTabViewCell
        print("cell = \(cell)")
        cell.delegate = self as ZipDelegate
        cell.filesPaths = (files[indexPath.row], true)
        
        return cell
    }
}


extension QQUnZipViewController: ZipDelegate {
    func startUnzip(cell: UnzipTabViewCell) {
        print("开始解压缩")
        if ZipTool.archiveZip(sourcePath:cell.filesPath.text!, destinationPath: FileTool.getTmp()) {
            print("success")
            print("path = \(FileTool.getTmp()); cell = \(cell.filesPath.text!)")
        }else {
            print("failed")
        }
    }
    
    
}










